package org.spring.aicloud.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

import java.util.concurrent.RejectedExecutionHandler;
import java.util.concurrent.ThreadPoolExecutor;

/**
 * @author yexiebao
 * @date 2025/5/22
 * 线程池配置
 */

@Configuration
public class ThreadPoolConfig {
    @Bean
    public ThreadPoolTaskExecutor threadPoolTaskExecutor(){
        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
        //核心线程数:+1是为了应对缓存未命中的情况，这样新的线程可以利用空闲cpu来执行任务
        executor.setCorePoolSize(Runtime.getRuntime().availableProcessors()+1);
        //最大线程数
        executor.setMaxPoolSize(Runtime.getRuntime().availableProcessors()*2+1);
        //任务队列的容量
        executor.setQueueCapacity(100000);
        //执行自定义拒绝策略
        executor.setRejectedExecutionHandler(new RejectedExecutionHandler() {
            @Override
            public void rejectedExecution(Runnable r, ThreadPoolExecutor executor) {
                // todo:1.保存拒绝任务
                // todo:2.通知相关负责人进行手动处理
            }
        });
        //初始化
        executor.initialize();
        return executor;
    }

}